package com.tmall.testgen.server.infocollect;

import java.util.Arrays;

import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.common.util.ConfigExtractor;

/**
 * @author haojia
 * @date 2014-07-04
 */
public class InfoCollectListener {

	
	private static  InterceptInfo initInterceptInfo(Class testClass,String methodName,String[] paramNames,Object[] paramValues,Object result){
		InterceptInfo info=new InterceptInfo();
		info.setClassName(testClass.getName());
		info.setClassSimpleName(testClass.getSimpleName());
		info.setParamNames(Arrays.asList(paramNames));
		info.setMethodName(methodName);
		info.setParams(Arrays.asList(paramValues));
		info.setResult(result);
		String beanName=ConfigExtractor.getBeanName(testClass);
		info.setBeanName(beanName);
		
		return info;
	}
	
	public static String getMethodParamContentContent(String[] paramNames,String[] paramTypes,String returnType){
		StringBuilder sb=new StringBuilder();
		if(paramTypes==null||paramTypes.length==0)
			return "( ):"+returnType;
		for(int i=0;i<paramNames.length;i++){
			sb.append(paramTypes[i]+" "+paramNames[i]+",");
		}
		
		String totalShowName=sb.toString();
		if(totalShowName.endsWith(",")){
			totalShowName=totalShowName.substring(0, totalShowName.length()-1);
		}
		
		
		return "("+totalShowName+"):"+returnType;
	}

	public static void addIntercept(Class testClass,String methodName,String paramNamesTem,String paramTypesTem,String returnType,Object[] paramValues,Object result){
	    String[] paramNames=paramNamesTem.split(";");
		String methodContent=getMethodParamContentContent(paramNames,paramTypesTem.split(";"),returnType);
		InterceptInfo info=initInterceptInfo(testClass,methodName, paramNames, paramValues, result);
		info.setMethodParamContent(methodContent);
		
		
		DataCenter.addIntercept(info);
	}
	
	
	public static void main(String[] args) {
		String s="";
		String[] ss=s.split(";");
		System.out.println(Arrays.asList(ss));
	}
}
